<include file='Index/header' />
<div class="row">
	<br>
	<div class="col-sm-12" >
    	<button type="button" data-toggle="modal" class="btn btn-primary" onclick="editNew()">新增公告</button>
    	<br><br>
   </div>

	<div class="col-sm-12">
		<div class="col-sm-2">
			<ul id="district_tree" class="ztree"></ul>
		</div>
		<div class="col-sm-10">
		<table id="notification_table" class="table table-hover table-bordered table-striped table-striped">
			<thead>
				<tr>
					<td>小区id</td>
					<td>编号</td>
					<td>公告名称</td>
					<td>发布时间</td>
					<td>排序</td>
					<td>操作</td>
				</tr>
			</thead>
			<tbody>
			</tbody>
		</table>
		</div>
	</div>
	<br>
</div>

<include file='Index/footer' />
<include file="District/notification-detail" />
<script>
$(document).ready(function() {
		loadDistrictTree('district_tree', true);
		loadTable();
});
var table;
function loadTable(){
	table = $('#notification_table').DataTable({
		"bProcessing": true,
		"bDestroy": true,
		"sPaginationType" : "input",
		"oLanguage" : {
              "sLengthMenu": "每页显示 _MENU_ 条记录",
              "sZeroRecords": "抱歉， 没有找到",
              "sInfo": "从 _START_ 到 _END_ /共 _TOTAL_ 条数据",
              "sInfoEmpty": "没有数据",
              "sInfoFiltered": "(从 _MAX_ 条数据中检索)",
              "sZeroRecords": "没有检索到数据",
               "sSearch": "搜索: &nbsp",
              "oPaginate": {
                "sFirst": "首页",
                "sPrevious": "前一页",
                "sNext": "后一页",
                "sLast": "尾页"
              }
          }, 
		"ajax":'{:U("Admin/District/getNotification/")}',
		"columns": [
					{ "data": 'district_id' },
	        { "data": 'id' },
	        { "data": 'title' },
	        { "data": 'createtime' },
	        { "data": 'index' },
	        { "data": 'btn' }
    ],
    "columnDefs": [
    {
    	"targets": 0,
    	"visible":false
    },
    {
    	"targets": 1,
    	"render": function (data, type, row, table) {
        return table.row + 1;
      }
    },
    {
    	"targets": -1,
    	"data": null,
    	"render": function (data, type, row, table) {
    		var btn = '暂停下单';
    		var status = '1';
    		if(row.status != '0'){
					btn = '开始下单';
					status = '0';
    		}
        return '<button class="btn btn-info" onclick="edit(\''+row.district_notification_id+'\')">编辑</button>  <button class="btn btn-danger" onclick="del(\''+row.district_notification_id+'\')">删除</button>';
      },
    }]
	});
}
var district_id = '';
function editNew(){
	var treeObj = $.fn.zTree.getZTreeObj("district_tree");
	var nodes = treeObj.getSelectedNodes();
	if(nodes.length == 0 || typeof(nodes[0].district_id) == 'undefined'){
    alert("请选择一个小区再执行此操作！");
    return;
  }
  //变量初始化
	district_id = nodes[0].district_id;
	district_notification_id = '';
  $('#notification_detail').modal({
		backdrop : "static"
	});
}
var district_notification_id = '';
function edit(district_notification_id){
	//编辑按钮点击，弹出编辑框
	$.ajax({
			url: '{:U("Admin/District/getNotification/")}',
			type: 'post',
			dataType: 'json',
			data:{
				"district_notification_id":district_notification_id
			}
		})
		.success(function(data){
			window.district_notification_id = data.district_notification_id;
			district_id = data.district_id;
			$("input[name='title']").attr('value', data.title);
			$("textarea[name='content']").html(data.content);
			$("input[name='index']").attr('value', data.index);

			$('#notification_detail').modal({
				backdrop : "static"
			});
	});
}

function del(district_notification_id){
	if(!confirm("确认删除该小区公告吗？")){
		return;
	}
	$.ajax({
			url: '{:U("Admin/District/deleteNotification/")}',
			type: 'post',
			dataType: 'json',
			data:{
				"district_notification_id":district_notification_id
			}
		})
		.success(function(data){
			location.reload();
	});
}

function searchDistrict(val){
	table.column(0).search('').draw();
	table.column(0).search(val).draw();
}

</script>
